﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlServerCe;
using System.Text;
using Entities;

namespace Distribution.Data
{
    public class DUser
    {
        #region - Public methods -

        public static List<EUser> GetAll(EVendor vendor)
        {
            SqlCeConnection _Con = null;
            StringBuilder query = new StringBuilder();

            try
            {
                _Con = Helper.GetConnection();

                query.Append("SELECT UserId, Name ");
                query.Append("FROM tb_Users ");
                query.Append("WHERE VendorFk = @VendorId");

                SqlCeCommand command = new SqlCeCommand(query.ToString(), _Con);
                command.CommandType = CommandType.Text;
                command.Parameters.Add(Helper.CreateIntParameter("@VendorId", ParameterDirection.Input, false, vendor.Id));

                SqlCeDataReader dr = command.ExecuteReader();

                List<EUser> users = new List<EUser>();

                while (dr.Read())
                {
                    EUser user = new EUser();

                    user.Id = dr.GetInt32(dr.GetOrdinal("UserId"));
                    user.Name = dr.GetString(dr.GetOrdinal("Name")); ;

                    users.Add(user);
                }

                return users;
            }
            finally
            {
                if (_Con != null)
                {
                    _Con.Close();
                }
            }
        }

        #endregion
    }
}
